package com.utils.info.util.hhy;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class H5SD01ElementParser {

    private static final int elm_code_length = 3;  // 元素代码  字符串 长度为 3
    private static final int elm_name_length = 20; // 元素名称 字符串，长度为20
    private static final int elm_act_length = 7;   // 元素值decimal (7,5)
    private static final int elm_act_precision = 5;
    private static final int loop_count = 100;


    public static List<Elm> parse(String str_50986,String str_50987,String str_50988) throws Exception {

        List<Elm> resultList = parseInternal(str_50986, str_50987, str_50988);
        System.out.println(resultList);
        return resultList;

    }

    /**
     *
     * @param str_50986  字符串为空，代表发送过来的内容全部为空格，按协议约定，解析后为空,为了便于处理，在收到宝信电文后已经做了空格去除工作
     * @param str_50987 字符串为空，代表发送过来的内容全部为空格，按协议约定，解析后为空,为了便于处理，在收到宝信电文后已经做了空格去除工作
     * @param str_50988 数值型，必须有内容
     * @return 实际解析后内容list，若未传输有效值，则返回空的list
     * * iXCom29d通信规约修订版.doc
     * 2.7 电文格式
     * 以下规约用于计算机间传送的电文：
    1）	电文中所有的数据都以字符形式传输和存贮，这样可以避免不同体系结构之间的字节序问题。
    以下是ASCII文本域的补位规则：
    数字数据：则在不满位数时左补0：
    例如：421：   “00421”在5位数字值的情况下
    -421：   “-0421”
    字符串数据：则在不够长度时右补空格
    例如：TSB：   “TSBDDDD”在7位字符串值的情况
     */
    private static List<Elm> parseInternal(String str_50986, String str_50987, String str_50988) throws Exception {

        verifyInput(str_50986, str_50987, str_50988, elm_code_length,elm_name_length,elm_act_length, loop_count);

        List<Elm> elmList = new ArrayList<Elm>(loop_count);
        // 实际有值，无值则返回一个空的list
        if(str_50986.length() != 0) {

            int realLoopCount = (int)Math.ceil(((double)str_50986.length() / elm_code_length));

            for (int i = 0; i < realLoopCount; i++) {
                int codeIndex = i * elm_code_length;
                int nameIndex = i * elm_name_length;
                int actIndex = i * elm_act_length;

                // 获得值
                String valueStr = str_50988.substring(actIndex, actIndex + elm_act_length);
                int splitIndex = valueStr.length() - elm_act_precision;
                String prev = valueStr.substring(0, splitIndex);
                String end = valueStr.substring(splitIndex);
                BigDecimal value = new BigDecimal(prev + "." + end);

                int codeEnd = codeIndex + elm_code_length;
                int nameEnd = nameIndex + elm_name_length;

                codeEnd = codeEnd > str_50986.length() ? str_50986.length() : codeEnd;
                nameEnd = nameEnd > str_50987.length() ? str_50987.length() : nameEnd;

                // 获得code name
                String code = str_50986.substring(codeIndex, codeEnd);
                String name = str_50987.substring(nameIndex, nameEnd);

                String realCode = code.trim();
                String realName = name.trim();

                Elm elm = new Elm(realCode, realName, value);

                elmList.add(elm);
            }
        }

        return elmList;
    }

    private static void verifyInput(String str_50986, String str_50987, String str_50988, int elm_code_length,
                                    int elm_name_length, int elm_act_length, int loop_count) {

        if(str_50986 == null || str_50987 == null || str_50988 == null) {
            throw new RuntimeException("input is null");
        }

        // 50988 因为传输的是数值，必须有值
        int length50988 = loop_count * elm_act_length;
        if(length50988 != str_50988.length()) {
            throw new RuntimeException("50988 长度不正确，期望长度:" + length50988 + ",实际长度:" + str_50988.length());
        }


        int length50986 = str_50986.length();
        int length50987 = str_50987.length();

        // 如果50986,50987 长度都为0，50988内容都必须为0
        if(length50986 == 0 && length50987 == 0) {

            char[] charArray = str_50988.toCharArray();
            ArrayList<Character> list = new ArrayList<Character>(charArray.length);
            for (int i = 0; i < charArray.length; i++) {
                list.add(charArray[i]);
            }
            Set<Character> setsChars = new HashSet<Character>(list);

            Character zeroCharacter = '0';
            if(setsChars.size() != 1 || setsChars.contains(zeroCharacter) == false) {
                throw new RuntimeException("50988 内容编码不正确");
            }

            return;
        }

        // 50986长度和50987长度有关系

        int elmCount50986 = (int)Math.ceil(((double)length50986 / elm_code_length));
        int elmCount50987 = (int)Math.ceil(((double)length50987 / elm_name_length));

        // 允许长度为0
        if(elmCount50986 != elmCount50987) {
            throw new RuntimeException("50986 和 50987 长度不匹配，其中50986长度为:" + length50986 + ",50987长度为:" + length50987);
        }
    }
}